package com.test.market.controller;

import com.github.pagehelper.PageInfo;
import com.test.market.pojo.Bill;
import com.test.market.pojo.Supplier;
import com.test.market.pojo.TableData;
import com.test.market.service.BillService;
import com.test.market.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徒有琴
 */
@Controller
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private SupplierService supplierService;

    //list.html?pn=1
    @RequestMapping("/list.html")
    public String list(Model model) {
        List<Supplier> supplierList = supplierService.getSupplierList(null);
        model.addAttribute("supplierList", supplierList);
        return "bill";
    }

    @RequestMapping("table.html")
    @ResponseBody
    public TableData<Bill> table(Bill bill, Integer page, Integer limit) {
        PageInfo<Bill> pageInfo = billService.getBillList(bill, page, limit);
        return new TableData<Bill>(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("edit.html")
    @ResponseBody
    public Map<String, Object> edit(Bill bill) {
        System.out.println(bill);
        Map<String, Object> result = new HashMap<>();
        try {
            if (bill.getId() == null) {
                //添加
                //调用service方法
            } else {
                //调用修改的方法
            }

            result.put("status", true);
            result.put("message", "编辑成功");
        } catch (Exception e) {
            result.put("status", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete.html")
    @ResponseBody
    public Map<String, Object> delete(int[] ids) {
        System.out.println(Arrays.toString(ids));
        Map<String, Object> result = new HashMap<>();
        try {
            //调用service方法
            result.put("status", true);
            result.put("message", "删除成功");
        } catch (Exception e) {
            result.put("status", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
